<?php
function leagueSort($a, $b) {
    if ($a->getPoints() != $b->getPoints()) {
      return $a->getPoints() > $b->getPoints() ? -1 : 1;
    }

    if ($a->currentSeasonStats->getNetTDs() != $b->currentSeasonStats->getNetTDs()) {
      return $a->currentSeasonStats->getNetTDs() > $b->currentSeasonStats->getNetTDs() ? -1 : 1;
    }

    if ($a->currentSeasonStats->getNetCas() != $b->currentSeasonStats->getNetCas()) {
      return $a->currentSeasonStats->getNetCas() > $b->currentSeasonStats->getNetCas() ? -1 : 1;
    }

    if ($a->currentSeasonStats->tdFor != $b->currentSeasonStats->tdFor) {
      return $a->currentSeasonStats->tdFor > $b->currentSeasonStats->tdFor? -1 : 1;
    }

    if ($a->currentSeasonStats->casFor != $b->currentSeasonStats->casFor) {
      return $a->currentSeasonStats->casFor > $b->currentSeasonStats->casFor? -1 : 1;
    }

    return $a->getTR() == $b->getTR() ? strcmp($a->name, $b->name) : $b->getTR() - $a->getTR();
}

function conferenceSort($a, $b) {
    if (!isset($a->conference) or !isset($b->conference) or 0 == strcmp($a->conference, $b->conference)) {
      return leagueSort($a, $b);
    } else {
      return strcmp($a->conference, $b->conference);
    }
}

function coachSort($a, $b) {
    if (!isset($a->conference) or !isset($b->conference) or 0 == strcmp($a->conference, $b->conference)) {
			$rc = strcasecmp($a->coach->name, $b->coach->name);
			if ($rc == 0) {
				$rc = leagueSort($a, $b);
			}
			return $rc;
		} else {
			return strcmp($a->conference, $b->conference);
    }
}

function teamNameSort($a, $b) {
    if (!isset($a->conference) or !isset($b->conference) or 0 == strcmp($a->conference, $b->conference)) {
		$rc = strcasecmp($a->name, $b->name);
		if ($rc == 0) {
			$rc = leagueSort($a, $b);
		}
		return $rc;
    } else {
      return strcmp($a->conference, $b->conference);
    }

}

function raceNameSort($a, $b) {
    if (!isset($a->conference) or !isset($b->conference) or 0 == strcmp($a->conference, $b->conference)) {
		$rc = strcasecmp($a->race->name, $b->race->name);
		if ($rc == 0) {
			$rc = leagueSort($a, $b);
		}
		return $rc;
    } else {
      return strcmp($a->conference, $b->conference);
    }

}

function tvSort($a, $b) {
    if (!isset($a->conference) or !isset($b->conference) or 0 == strcmp($a->conference, $b->conference)) {
		$rc = $b->getTR() - $a->getTR();
		if ($rc == 0) {
			$rc = leagueSort($a, $b);
		}
		return $rc;
    } else {
      return strcmp($a->conference, $b->conference);
    }

}

function playedSort($a, $b) {
    if (!isset($a->conference) or !isset($b->conference) or 0 == strcmp($a->conference, $b->conference)) {
    $rc = $b->currentSeasonStats->played - $a->currentSeasonStats->played;
		if ($rc == 0) {
			$rc = leagueSort($a, $b);
		}
		return $rc;
    } else {
      return strcmp($a->conference, $b->conference);
    }

}

function wonSort($a, $b) {
    if (!isset($a->conference) or !isset($b->conference) or 0 == strcmp($a->conference, $b->conference)) {
		$rc = $b->currentSeasonStats->won - $a->currentSeasonStats->won;
		if ($rc == 0) {
			$rc = leagueSort($a, $b);
		}
		return $rc;
    } else {
      return strcmp($a->conference, $b->conference);
    }

}

function drawnSort($a, $b) {
    if (!isset($a->conference) or !isset($b->conference) or 0 == strcmp($a->conference, $b->conference)) {
		$rc = $b->currentSeasonStats->drawn - $a->currentSeasonStats->drawn;
		if ($rc == 0) {
			$rc = leagueSort($a, $b);
		}
		return $rc;
    } else {
      return strcmp($a->conference, $b->conference);
    }

}

function lostSort($a, $b) {
    if (!isset($a->conference) or !isset($b->conference) or 0 == strcmp($a->conference, $b->conference)) {
		$rc = $b->currentSeasonStats->lost - $a->currentSeasonStats->lost;
		if ($rc == 0) {
			$rc = leagueSort($a, $b);
		}
		return $rc;
    } else {
      return strcmp($a->conference, $b->conference);
    }

}

// Net TD's
function leagueNetTdsSort($a, $b) {
    if (!isset($a->conference) or !isset($b->conference) or 0 == strcmp($a->conference, $b->conference)) {
		$rc = $b->currentSeasonStats->getNetTds() - $a->currentSeasonStats->getNetTds();
		if ($rc == 0) {
			$rc = leagueSort($a, $b);
		}
		return $rc;
    } else {
      return strcmp($a->conference, $b->conference);
    }
}

// Net cas
function leagueNetCasSort($a, $b) {
    if (!isset($a->conference) or !isset($b->conference) or 0 == strcmp($a->conference, $b->conference)) {
		$rc = $b->currentSeasonStats->getNetCas() - $a->currentSeasonStats->getNetCas();
		if ($rc == 0) {
			$rc = leagueSort($a, $b);
		}
		return $rc;
    } else {
      return strcmp($a->conference, $b->conference);
    }
}

// Net combined
function leagueNetCombinedSort($a, $b) {
    if (!isset($a->conference) or !isset($b->conference) or 0 == strcmp($a->conference, $b->conference)) {
		$rc = $b->currentSeasonStats->getNetCas() - $a->currentSeasonStats->getNetCas() + $b->currentSeasonStats->getNetTds() - $a->currentSeasonStats->getNetTds();
		if ($rc == 0) {
			$rc = leagueSort($a, $b);
		}
		return $rc;
    } else {
      return strcmp($a->conference, $b->conference);
    }
}


function winsSort($a, $b) {
	$a->getStats();
	$b->getStats();
    return $b->stats->wins - $a->stats->wins;
}

function noLossesSort($a, $b) {
	$a->getStats();
	$b->getStats();
    return $b->stats->noLoss - $a->stats->noLoss;
}

function noWinsSort($a, $b) {
	$a->getStats();
	$b->getStats();
    return $a->stats->noWin - $b->stats->noWin;
}

function lossesSort($a, $b) {
	$a->getStats();
	$b->getStats();
    return $a->stats->loss - $b->stats->loss;
}

function tempWinsSort($a, $b) {
	$a->getStats();
	$b->getStats();
    return $b->stats->currentWins - $a->stats->currentWins;
}

function tempNoLossesSort($a, $b) {
	$a->getStats();
	$b->getStats();
    return $b->stats->currentNoLoss - $a->stats->currentNoLoss;
}

function tempNoWinsSort($a, $b) {
	$a->getStats();
	$b->getStats();
    return $a->stats->currentNoWin - $b->stats->currentNoWin;
}

function tempLossesSort($a, $b) {
	$a->getStats();
	$b->getStats();
    return $a->stats->currentLoss - $b->stats->currentLoss;
}

// Total TD's scored
function totalTdScoredSort($a, $b) {
    return $b->currentSeasonStats->tdFor - $a->currentSeasonStats->tdFor;
}

// Total TD's conceded
function totalTdConcededSort($a, $b) {
    return $a->currentSeasonStats->tdAg - $b->currentSeasonStats->tdAg;
}

// Net TD's
function totalNetTdsSort($a, $b) {
    $rc = $b->currentSeasonStats->getNetTds() - $a->currentSeasonStats->getNetTds();
    if ($rc == 0) {
    	$rc = leagueSort($a, $b);
}
    return $rc;
}

// Total cas inflicted
function totalCasScoredSort($a, $b) {
    return $b->currentSeasonStats->casFor - $a->currentSeasonStats->casFor;
}

// Total cas suffered
function totalCasConcededSort($a, $b) {
    return $a->currentSeasonStats->casAg - $b->currentSeasonStats->casAg;
}

// Net cas
function totalNetCasSort($a, $b) {
    $rc = $b->currentSeasonStats->getNetCas() - $a->currentSeasonStats->getNetCas();
    if ($rc == 0) {
    	$rc = leagueSort($a, $b);
}
    return $rc;
}

// Average TD's scored
function aveTdScoredSort($a, $b) {
    return $b->currentSeasonStats->getAverageScore() == $a->currentSeasonStats->getAverageScore() ? 0 : 
    $a->currentSeasonStats->getAverageScore() > $b->currentSeasonStats->getAverageScore() ? -1 : 1;
}

// Average TD's conceded
function aveTdConcededSort($a, $b) {
    return $a->currentSeasonStats->getAverageConceded() == $b->currentSeasonStats->getAverageConceded() ? 0 : 
    $b->currentSeasonStats->getAverageConceded() > $a->currentSeasonStats->getAverageConceded() ? -1 : 1;
}

// Average Net TD's
function aveNetTdsSort($a, $b) {
    return $b->currentSeasonStats->getAverageNetTD() == $a->currentSeasonStats->getAverageNetTD() ? 0 : 
    $a->currentSeasonStats->getAverageNetTD() > $b->currentSeasonStats->getAverageNetTD() ? -1 : 1;
}

// Average cas inflicted
function aveCasScoredSort($a, $b) {
    return $b->currentSeasonStats->getAverageInflicted() == $a->currentSeasonStats->getAverageInflicted() ? 0 : 
    $a->currentSeasonStats->getAverageInflicted() > $b->currentSeasonStats->getAverageInflicted() ? -1 : 1;
}

// Average cas suffered
function aveCasConcededSort($a, $b) {
    return $a->currentSeasonStats->getAverageSuffered() == $b->currentSeasonStats->getAverageSuffered() ? 0 : 
    $b->currentSeasonStats->getAverageSuffered() > $a->currentSeasonStats->getAverageSuffered() ? -1 : 1;
}

// Average Net cas
function aveNetCasSort($a, $b) {
    return $b->currentSeasonStats->getAverageNetCas() == $a->currentSeasonStats->getAverageNetCas() ? 0 : 
    $a->currentSeasonStats->getAverageNetCas() > $b->currentSeasonStats->getAverageNetCas() ? -1 : 1;
}

// Best td scored
function bestTDScoredSort($a, $b) {
    return $b->currentSeasonStats->highTdFor->myValue - $a->currentSeasonStats->highTdFor->myValue;
}

// Best td conceded
function bestTDConcededSort($a, $b) {
    return $a->currentSeasonStats->lowOppoTdFor->myValue - $b->currentSeasonStats->lowOppoTdFor->myValue;
}

// Worst td scored
function worstTDScoredSort($a, $b) {
    return $b->currentSeasonStats->lowTdFor->myValue - $a->currentSeasonStats->lowTdFor->myValue;
}

// Worst td conceded
function worstTDConcededSort($a, $b) {
    return $a->currentSeasonStats->highOppoTdFor->myValue - $b->currentSeasonStats->highOppoTdFor->myValue;
}

// Best Cas scored
function bestCasScoredSort($a, $b) {
    return $b->currentSeasonStats->highCasFor->myValue - $a->currentSeasonStats->highCasFor->myValue;
}

// Best Cas conceded
function bestCasConcededSort($a, $b) {
    return $a->currentSeasonStats->lowOppoCasFor->myValue - $b->currentSeasonStats->lowOppoCasFor->myValue;
}

// Worst Cas scored
function worstCasInflictedSort($a, $b) {
    return $b->currentSeasonStats->lowCasFor->myValue - $a->currentSeasonStats->lowCasFor->myValue;
}

// Worst Cas conceded
function worstCasSufferedSort($a, $b) {
    return $a->currentSeasonStats->highOppoCasFor->myValue - $b->currentSeasonStats->highOppoCasFor->myValue;
}

function echoTeamSelect($selectName, &$teams, $teamId, $loggedInId) {
	echo "<td><select name=\"" . $selectName . "\">";
	uasort ($teams->teams, "coachSort");
	foreach ($teams->teams as $team) {
		echo "<option value=\"" . $team->id . "\"";
		if (isset($teamId)) {
			if ($teamId == $team->id) {
				echo " selected ";
			}
		} else if ($loggedInId == $team->coach->id) {
			echo " selected ";
		}
		echo ">". $team->name . " (" . $team->coach->name . ")</option>\n";
	}
	echo "</select></td>";
}

?>